<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:svg="http://www.w3.org/2000/svg">
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=450930
-->
<head>
  <title>Test for Bug 450930 (MozAfterPaint)</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="runNext()">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=450930">Mozilla Bug 450930</a>
<div id="display">
  <div id="d" style="width:400px; height:200px;"></div>
  <iframe id="iframe" style="width:400px; height:200px;"
   src="data:text/html,&lt;div id='d'&gt;&lt;span style='margin-left:3px;'&gt;Hello&lt;/span&gt;
        &lt;/div&gt;&lt;div style='margin-top:500px' id='d2'&gt;
        &lt;span style='margin-left:3px;'&gt;Goodbye&lt;/span&gt;&lt;/div>"></iframe>
  <svg:svg style="width:410px; height:210px;" id="svg">
    <svg:foreignObject width="100%" height="100%">
      <iframe id="iframe2" style="width:400px; height:200px;"
       src="data:text/html,&lt;div id='d'&gt;&lt;span style='margin-left:3px;'&gt;Hello&lt;/span&gt;
            &lt;/div&gt;&lt;div style='margin-top:500px' id='d2'&gt;
            &lt;span style='margin-left:3px;'&gt;Goodbye&lt;/span&gt;&lt;/div>"></iframe>
    </svg:foreignObject>
  </svg:svg>
</div>
<div id="content" style="display: none">
</div>


<pre id="test">
<script class="testbody" type="text/javascript"><![CDATA[

function flash(doc, name) {
  var d = doc.getElementById(name);
  d.style.backgroundColor = d.style.backgroundColor == "blue" ? "yellow" : "blue";
  // Now flush out style changes in that document, since our event listeners
  // seem to assume that things will work that way.
  d.getBoundingClientRect();
}

function le(v1, v2, s) {
  window.opener.ok(v1 <= v2, s + " (" + v1 + "," + v2 + ")");
}

function checkContains(r1, r2, s) {
  le(Math.round(r1.left), Math.round(r2.left), "Left edges out" + s);
  le(Math.round(r2.right), Math.round(r1.right), "Right edges out" + s);
  le(Math.round(r1.top), Math.round(r2.top), "Top edges out" + s);
  le(Math.round(r2.bottom), Math.round(r1.bottom), "Bottom edges out" + s);
}

function isRect(r1, r2) {
  return (Math.abs(r1.left - r2.left) <= 1 ||
          Math.abs(r1.right - r2.right) <= 1 ||
          Math.abs(r1.top - r2.top) <= 1 ||
          Math.abs(r1.bottom - r2.bottom) <= 1);
}

function isRectInList(r, list) {
  for (var i = 0; i < list.length; ++i) {
    if (isRect(r, list[i]))
      return true;
  }
  return false;
}

function doesRectContain(r1, r2) {
  return Math.floor(r1.left) <= r2.left && r2.right <= Math.ceil(r1.right) &&
         Math.floor(r1.top) <= r2.top && r2.bottom <= Math.ceil(r1.bottom);
}

function rectToString(r) {
  return "(" + r.left + "," + r.top + "," + r.right + "," + r.bottom + ")";
}

function doesRectContainListElement(r, list) {
  dump("Incoming rect: " + rectToString(r) + "\n");
  for (var i = 0; i < list.length; ++i) {
    dump("List rect " + i + ": " + rectToString(list[i]));
    if (doesRectContain(r, list[i])) {
      dump(" FOUND\n");
      return true;
    }
    dump("\n");
  }
  dump("NOT FOUND\n");
  return false;
}

function checkGotSubdoc(list, container) {
  var r = container.getBoundingClientRect();
  return doesRectContainListElement(r, list);
}

function runTest1() {
  // test basic functionality
  var iterations = 0;
  var foundExactRect = false;

  function listener(event) {
    var r = SpecialPowers.wrap(event).boundingClientRect;
    var bounds = document.getElementById('d').getBoundingClientRect();
    checkContains(r, bounds, "");
    if (isRectInList(bounds, SpecialPowers.wrap(event).clientRects)) {
      foundExactRect = true;
    }
    window.removeEventListener("MozAfterPaint", listener, false);
    ++iterations;
    if (iterations < 4) {
      setTimeout(triggerPaint, 100);
    } else {
      window.opener.ok(foundExactRect, "Found exact rect");
      runNext();
    }
  }

  function triggerPaint() {
    window.addEventListener("MozAfterPaint", listener, false);
    flash(document, 'd');
    window.opener.ok(true, "trigger test1 paint");
  }
  triggerPaint();
}

function runTest2(frameID, containerID) {
  // test reporting of painting in subdocuments
  var fired = 0;
  var gotSubdocPrivileged = false;
  var iframe = document.getElementById(frameID);
  var container = document.getElementById(containerID);

  function listener(event) {
    if (checkGotSubdoc(SpecialPowers.wrap(event).clientRects, container))
      gotSubdocPrivileged = true;
    if (SpecialPowers.wrap(event).clientRects.length > 0) {
      if (++fired == 1)
        setTimeout(check, 100);
    }
  }

  function check() {
    window.opener.is(fired, 1, "Wrong event count (" + frameID + ")");
    window.opener.ok(gotSubdocPrivileged, "Didn't get subdoc invalidation while we were privileged (" + frameID + ")");
    window.removeEventListener("MozAfterPaint", listener, false);
    runNext();
  }

  function triggerPaint() {
    window.addEventListener("MozAfterPaint", listener, false);
    document.body.offsetTop;
    flash(iframe.contentDocument, 'd');
  }
  triggerPaint();
}

var test = 0;
var tests = [runTest1,
             function() { runTest2("iframe", "iframe") },
             function() { runTest2("iframe2", "svg") }];
function runNext() {
  if (SpecialPowers.DOMWindowUtils.isMozAfterPaintPending) {
    // Wait until there are no pending paints before trying to run tests
    setTimeout(runNext, 100);
    return;
  }
  if (test < tests.length) {
    ++test;
    tests[test - 1]();
  } else {
    window.opener.finishTests();
  }
}


]]></script>
</pre>

</body>
</html>
